package zyc.java.project.springbootprojectweb.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import zyc.java.project.springbootprojectweb.dao.UserDao;
import zyc.java.project.springbootprojectweb.dao.impl.UserDaoImpl;
import zyc.java.project.springbootprojectweb.pojo.User;
import zyc.java.project.springbootprojectweb.service.UserService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final UserDao userDao;

    @Override
    public List<User> findAll() {
        List<String> lines = userDao.findAll(); // 获取原始数据中的每一行

        List<User> userList = lines.stream().map(
                line -> {
                    String[] split = line.split(",");
                    Integer id = Integer.valueOf(split[0]);
                    String userName = split[1];
                    String password = split[2];
                    String name = split[3];
                    Integer age = Integer.valueOf(split[4]);
                    LocalDateTime updateTime = LocalDateTime.parse(split[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // 按照给定格式解析
                    return new User(id, userName, password, name, age, updateTime);
                }
        ).toList();

        return userList;
    }

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }
}
